<?php

class M_shiftkerja extends CI_Model{
	
	function M_shiftkerja(){
		parent::__construct();
	}
	
	function get_list(){
		$query = "SELECT NAMASHIFT, VALIDTO, KETERANGAN
		FROM shift";
			
		$result = $this->db->query($query);
		$nbrows = $result->num_rows();
	
		$jsonresult = "";
		$data = array();
		if($nbrows>0){
			$i=0;
			foreach($result->result() as $row){
				$data[$i] = $row;
				$i++;
			}
			return "{\"data\":" .json_encode($data). "}";
		} else {
			return "{\"data\":" .$jsonresult. "}";
		}
	}
	
	function create($namashift,$validto,$keterangan){
		$sql = "SELECT * FROM shift WHERE NAMASHIFT='".$namashift."'";
		$rs = $this->db->query($sql);
		if($rs->num_rows()>0){
			return 0;
		}else{
			$data = array(
					"NAMASHIFT"=>$namashift,
					"VALIDTO"=>$validto,
					"KETERANGAN"=>$keterangan
			);
			$this->db->insert('shift', $data);
			if($this->db->affected_rows()){
				return 1;
			}else{
				return 0;
			}
		}
	}
	
	function update($namashift,$validto,$keterangan){
		$data = array(
				"NAMASHIFT"=>$namashift,
				"VALIDTO"=>$validto,
				"KETERANGAN"=>$keterangan
			);
		$this->db->where('NAMASHIFT', $namashift);
		$this->db->update('shift', $data);
		
		return 1;
	}
	
	function delete($namashift){
		$this->db->where('NAMASHIFT', $namashift);
		$this->db->delete('shift');
		
		return 1;
	}
	
}

?>